package com.xhwl.logistics.service.cent;

import com.xhwl.common.dto.cent.device.DeviceDTO;
import com.xhwl.common.enums.CategoryEnum;
import com.xhwl.common.enums.CodeEnum;
import com.xhwl.common.exception.BusinessException;
import com.xhwl.common.pojo.ResultJson;
import com.xhwl.common.pojo.cent.device.Device;
import com.xhwl.common.query.cent.device.DevicePageQuery;
import com.xhwl.logistics.bo.cent.DeviceBO;
import com.xhwl.logistics.bo.cent.VehicleIntegratedConfigBO;
import com.xhwl.logistics.bo.cent.VehicleParkingInfoBO;
import com.xhwl.logistics.bo.cent.VehicleSystemInfoBO;
import com.xhwl.logistics.component.SignForCoSon;
import com.xhwl.logistics.converter.mapstruct.CentDeviceConverter;
import com.xhwl.logistics.dto.ResponseCommonPushCoSonDTO;
import com.xhwl.logistics.dto.push.CarDeviceDataCoSonDTO;
import com.xhwl.logistics.dto.push.VehicleleListCoSonDTO;
import com.xhwl.logistics.service.IVehcDeviceService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

//@Service
public class QueryDeviceSecondService {

    @Autowired
    private IDeviceService deviceService;

    @Autowired
    private CentDeviceConverter centDeviceConverter;
    @Autowired
    private SignForCoSon signForCoSon;
    @Autowired
    private QueryDeviceService queryDeviceService;
    @Autowired
    private IVehcDeviceService vehcDeviceService;

    private void centDeviceResponseHandle(ResultJson<?> resultJson, String message) {
        if (!(resultJson.getState() && resultJson.getCode().equals(CodeEnum.SUCCESS.code))) {
            throw new BusinessException(message);
        }
    }

    public List<DeviceBO> queryDevicesByProjectId(Integer projectId) {
        DevicePageQuery devicePageQuery = new DevicePageQuery();
        devicePageQuery.setProjectId(projectId);
        devicePageQuery.setCategoryId(CategoryEnum.YARD_GATE.getId());

        List<DeviceDTO> deviceDTOList = deviceService.listDevice(null, devicePageQuery);
        return centDeviceConverter.deviceDTOList2DeviceBOList(deviceDTOList);
    }

    public DeviceBO queryDevicesByProjectIdAndDeviceNumber(Integer projectId, String number) {
        DevicePageQuery devicePageQuery = new DevicePageQuery();
        devicePageQuery.setProjectId(projectId);
        devicePageQuery.setCategoryId(CategoryEnum.YARD_GATE.getId());
        devicePageQuery.setIotDeviceId(number);

        List<DeviceDTO> deviceDTOList = deviceService.listDevice(null, devicePageQuery);
        if (deviceDTOList.isEmpty()) {
            throw new IllegalStateException("根据项目id和设备number找不到设备");
        } else if (deviceDTOList.size() > 1) {
            throw new IllegalStateException("根据项目id和设备number找到多个设备");
        } else {
            return centDeviceConverter.deviceDTO2DeviceBO(deviceDTOList.get(0));
        }
    }

    /**
     * 通过第三方设备id和项目id来查询设备信息
     */
    public DeviceBO queryDeviceByProjectIdAndThirdDeviceId(Integer projectId, String thirdDeviceId){
        DevicePageQuery devicePageQuery = new DevicePageQuery();
        devicePageQuery.setProjectId(projectId);
        devicePageQuery.setCategoryId(CategoryEnum.YARD_GATE.getId());
        devicePageQuery.setThirdDeviceId(thirdDeviceId);

        List<DeviceDTO> deviceDTOS = deviceService.listDevice(null, devicePageQuery);
        if(deviceDTOS.isEmpty()){
            throw new IllegalStateException("根据项目id和第三方设备id找不到设备");
        }else if (deviceDTOS.size() > 1){
            throw new IllegalStateException("根据项目id和第三方设备id找到多个设备");
        }else{
            return centDeviceConverter.deviceDTO2DeviceBO(deviceDTOS.get(0));
        }
    }

    public void updateDevices(List<DeviceBO> deviceBOList) {
        ResultJson<?> resultJson = deviceService.batchUpdate(null, centDeviceConverter.deviceBOList2DeviceDTOList(deviceBOList));
        centDeviceResponseHandle(resultJson, "feign调用设备中台的批量更新出错");
    }

    public void updateDeviceStatus(List<DeviceBO> deviceBOList) {
        deviceService.asyncUpdateOnlineStatus(centDeviceConverter.deviceBOList2DeviceList(deviceBOList));
    }
    public void updateDeviceStatus(Device device) {
        deviceService.asyncUpdateOnlineStatus(Collections.singletonList(device));
    }

    public void addDevices(List<DeviceBO> deviceBOList) {
        ResultJson<?> resultJson = deviceService.batchSave(null, centDeviceConverter.deviceBOList2DeviceList(deviceBOList));
        centDeviceResponseHandle(resultJson, "feign调用设备中台的批量新增出错");
    }

    public VehicleIntegratedConfigBO addDeviceCoSon(CarDeviceDataCoSonDTO carDeviceDataCoSonDTO){
        DeviceBO deviceBO = new DeviceBO();
        Integer size = carDeviceDataCoSonDTO.getVehichleList().size();
        int i;
        //获取设备信息
        for(i=0;i<size;i++)
        {
            List<VehicleleListCoSonDTO> vehichleList= carDeviceDataCoSonDTO.getVehichleList();
            deviceBO.setName(carDeviceDataCoSonDTO.getSentrybox_No());
            deviceBO.setPosition(vehichleList.get(i).getVlName());
            if(null != vehichleList.get(i).getVlvideoIp()){
                deviceBO.setNumber(vehichleList.get(i).getVlvideoIp());
            }
            else{
                deviceBO.setNumber(vehichleList.get(i).getVlctrlIp());
            }
            deviceBO.setNumber(vehichleList.get(i).getVlvideoIp());
            VehicleIntegratedConfigBO vehicleIntegratedConfigBO = queryDeviceService.queryIntegratedConfigFromParkId(carDeviceDataCoSonDTO.getKey());
            VehicleSystemInfoBO vehicleSystemInfoBO = vehicleIntegratedConfigBO.getVehicleSystemInfoBO();
            deviceBO.setProjectId(vehicleSystemInfoBO.getProjectId());
            deviceBO.setProductId(vehicleSystemInfoBO.getProductId());
            deviceBO.setCategoryId(vehicleSystemInfoBO.getProductCategoryId());
            List<DeviceBO> deviceBOList = new LinkedList<>();
            deviceBOList.add(deviceBO);

            DevicePageQuery devicePageQuery =new DevicePageQuery();
            devicePageQuery.setProjectId(vehicleSystemInfoBO.getProjectId());
            List<Device> list = vehcDeviceService.listdevice(devicePageQuery,null);

            Integer devicesize = list.size();
            if (devicesize !=0 && list.equals(centDeviceConverter.deviceBOList2DeviceList(deviceBOList))){
            List<DeviceDTO> deviceDTOList = centDeviceConverter.deviceBOList2DeviceDTOList(deviceBOList);
            ResultJson<?> resultJson = deviceService.batchUpdate(null, deviceDTOList);
            centDeviceResponseHandle(resultJson, "feign调用设备中台的批量更新出错");
            }
            else{
                ResultJson<?> resultJson = deviceService.batchSave(null, centDeviceConverter.deviceBOList2DeviceList(deviceBOList));
                centDeviceResponseHandle(resultJson, "feign调用设备中台的新增出错");
            }
        }

        VehicleIntegratedConfigBO vehicleIntegratedConfigBO = queryDeviceService.queryIntegratedConfigFromParkId(carDeviceDataCoSonDTO.getKey());
        VehicleParkingInfoBO vehicleParkingInfoBO = vehicleIntegratedConfigBO.getVehicleParkingInfoBO();
        ResponseCommonPushCoSonDTO responseCommonPushCoSonDTO = new ResponseCommonPushCoSonDTO();
        signForCoSon.sign(responseCommonPushCoSonDTO,"1","ok",vehicleParkingInfoBO);


        return vehicleIntegratedConfigBO;
    }
}
